import { decompression } from "@/request/upload";
import { message } from 'ant-design-vue';
import { Video } from "@/util/videoUrlFormat";
/**
 * @param { fileUrl: string }
 * @description 解压文件
 */
export function Decompression(fileUrl, unzipFilePath) {
  return new Promise((resolve) => {
    let params = {
      fileUrl,
      unzipFilePath
    };
    if (!params.fileUrl) {
      message.error({
        content: "获取文件路径失败！",
      });
      return resolve(false);
    }
    decompression(params).then((res) => {
      if (res.errno === 0 && res.data?.filePaths?.length !== 0) {
        return resolve(res)
      } else {
        message.error({
          content: res.message || "解压失败！",
        });
        return resolve(false)
      }
    });
  })
}
// 递归文件路径
export function recursionFilePath(data, baseFileUrl, that) {
  if (!data.fileUrl) data.fileUrl = data.fileName;
  data.fileUrl = baseFileUrl ? baseFileUrl + data.fileUrl : data.fileUrl;
  if (data.isDir) {
    let arr = [];
    data.fileUrl = baseFileUrl ? baseFileUrl + "/" + data.fileName : "/" + data.fileName;
    data.subFiles.forEach((fitem) => {
      if (fitem.isDir) {
        fitem.fileUrl = fitem.fileName;
        if (data.fileUrl.indexOf("http://") !== -1 || data.fileUrl.indexOf("https://") !== -1) arr.push(Object.assign({}, fitem))
        else arr.push(
          recursionFilePath(
            Video.urlFormat(Object.assign({}, fitem), data.fileUrl),
            data.fileUrl,
            that
          )
        );
      } else {
        fitem.fileUrl = fitem.fileName;
        if (data.fileUrl.indexOf("http://") !== -1 || data.fileUrl.indexOf("https://") !== -1) arr.push(Object.assign({}, fitem))
        else arr.push(Video.urlFormat(Object.assign({}, fitem), data.fileUrl));
      }
    });
    that.$set(data, "subFiles", arr);
    return Object.assign({}, data);
  } else {
    return Video.urlFormat(data, baseFileUrl);
  }
}